from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
import datetime

#Ratings can be attached generically to most any object.
#Users can only attach one rating per object
class BaseRatingAbstractModel(models.Model):

	content_type   = models.ForeignKey(ContentType,
			verbose_name=_('content type'),
			related_name="content_type_set_for_%(class)s")
	object_pk      = models.TextField(_('object ID'))
	content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
	user = models.ForeignKey(User, verbose_name=_('user'),
			blank=True, null=True, related_name="%(class)s_ratings")
	user_name = models.CharField(_("user's name"), max_length=50, blank=True)
	
	class Meta:
        unique_together = [('user', 'content_type', 'object_pk')]
        verbose_name = _('rating')
        verbose_name_plural = _('ratings')
	
	def __unicode__(self):
			return "%s: %s" % (self.user, self.rate_type)
		

#Now For Each Rating Type Available in pirate-politics
#The Specific Rating used is determined outside this scope
#Ratings are assumed to be attached by the author

class ProConNeutRating(BaseRatingAbstractModel):
	CHOICES = (
		('Pro', 'Provide Support For'),
		('Con', 'Provide Argument Against'),
		('Neutral', 'Neither here nor there'),
	)
	rate_type = models.CharField(max_length=7, choices=CHOICES)
	
	def __unicode__(self):
	        return "%s: %s" % (self.user, self.rate_type)
	
	def save(self, force_insert=False, force_update=False):
		if self.submit_date is None:
			self.submit_date = datetime.datetime.now()
		super(ProConNeutRating, self).save(force_insert, force_update)
	
class StarRating(BaseRatingAbstractModel):
	CHOICES = (
		('1', '1 Star'),
		('2', '2 Stars'),
		('3', '3 Stars'),
		('4', '4 Stars'),
		('5', '5 Stars'),
	)
	rate_type = models.CharField(max_length=1, choices=CHOICES)
	
	def __unicode__(self):
	        return "%s: %s" % (self.user, self.rate_type)
	
	def save(self, force_insert=False, force_update=False):
		if self.submit_date is None:
			self.submit_date = datetime.datetime.now()
		super(StarRating, self).save(force_insert, force_update)